package com.cwk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.cwk.yygh.hosp.repository.DepartmentRepository;

import com.cwk.yygh.hosp.service.DepartmentService;
import com.cwk.yygh.model.hosp.Department;

import com.cwk.yygh.vo.hosp.DepartmentQueryVo;
import com.cwk.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Repository;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> paramMap) {
        String mapString = JSONObject.toJSONString(paramMap);
        Department department =  JSONObject.parseObject(mapString, Department.class);
        String hoscode=department.getHoscode();
        String depcode=department.getDepcode();
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        if(departmentExist!=null){
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Department getDepartmentByHoscodeAndDepcode(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        return department;
    }

    @Override
    public Page<Department> findDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit);
        Department department = new Department();
        department.setHoscode(departmentQueryVo.getHoscode());
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void removeDepartment(String depcode, String hoscode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDepartmentByHoscode(String hoscode) {
        List<Department> departmentList=departmentRepository.findByHoscode(hoscode);
        List<DepartmentVo> departmentVoList=new ArrayList<>();
        Map<String, List<Department>> collect = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()){
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(entry.getKey());
            departmentVo.setDepname(entry.getValue().get(0).getBigname());
            List<DepartmentVo> children=new ArrayList<>();
            for (Department department:entry.getValue()){
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                children.add(departmentVo1);
            }
            departmentVo.setChildren(children);
            departmentVoList.add(departmentVo);
        }
        return departmentVoList;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        if(department!=null){
            return department.getDepname();
        }
        return "";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
